package com.lbtc.service.Impl;

import com.lbtc.Utils.MD5Util;
import com.lbtc.common.ApiRestResponse;
import com.lbtc.dao.UserMapper;
import com.lbtc.domian.User;
import com.lbtc.exception.LbtcMallException;
import com.lbtc.exception.LbtcMallExceptionEnum;
import com.lbtc.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.security.NoSuchAlgorithmException;

@Service
public class UserServiceImpl implements UserService {
    @Autowired
    private UserMapper userMapper;
    @Override
    public User getUserById(Integer id) {
        return  userMapper.selectByPrimaryKey(id);
    }

    @Override
    public void registUser(String username,String password) throws LbtcMallException {
        User user = userMapper.selectByName(username);
        if (user != null) {
            throw new LbtcMallException(LbtcMallExceptionEnum.USER_EXISTED);
        }
        User user1 = new User();
        user1.setUsername(username);
        try {
            user1.setPassword(MD5Util.getMD5value(password));
        } catch (NoSuchAlgorithmException e) {
            e.printStackTrace();
        }
        int i = userMapper.insertSelective(user1);
        if (i==0) {
            throw new LbtcMallException(LbtcMallExceptionEnum.INSERT_FAILED);
        }
    }

    @Override
    public User login(User user) throws LbtcMallException {
        User selectByName = userMapper.selectByName(user.getUsername());
        if (selectByName == null) {//用户名不存在
            throw  new LbtcMallException(LbtcMallExceptionEnum.LOGIN_NOT_EXIT_USERNAME);
        }
        //用户密码转为MD5密码与数据库存的密码比较
        String md5Password=null;
        try {
            md5Password = MD5Util.getMD5value(user.getPassword());
        } catch (NoSuchAlgorithmException e) {
            e.printStackTrace();
        }
        User dbUser = userMapper.selectByNameOrPassword(user.getUsername(), md5Password);
        if (dbUser==null) {//密码错误
            throw new LbtcMallException(LbtcMallExceptionEnum.LOGIN_ERROR_PASSWORD);
        }
        return dbUser;
    }
    @Override
    public User lbtcLogin(String username,String password) throws LbtcMallException {
        User selectByName = userMapper.selectByName(username);
        if (selectByName == null) {//用户名不存在
            throw  new LbtcMallException(LbtcMallExceptionEnum.LOGIN_NOT_EXIT_USERNAME);
        }
        //用户密码转为MD5密码与数据库存的密码比较
        String md5Password=null;
        try {
            md5Password = MD5Util.getMD5value(password);
        } catch (NoSuchAlgorithmException e) {
            e.printStackTrace();
        }
        User dbUser = userMapper.selectByLogin(username, md5Password);
        if (dbUser==null) {//密码错误
            throw new LbtcMallException(LbtcMallExceptionEnum.LOGIN_ERROR_PASSWORD);
        }//登录成功
        return dbUser;
    }

    @Override
    public User updateInformation(User user) throws LbtcMallException {
        int i = userMapper.updateByPrimaryKeySelective(user);//动态sql更新
        System.out.println("成功条数"+i);
        if (i<0) {
            throw new LbtcMallException(LbtcMallExceptionEnum.ERROR_UPDATE);
        }
        System.out.println("更新数据"+user);
        return user;
    }

    @Override
    public boolean checkAdminRole(User user) {
        //1是普通用户，2是管理员
        return user.getRole().equals(2);
    }
}
